<?php
function attrAlignment(){
  global $_GET, $data, $path, $style, $session_id, $language, $header, $term_Thing_ID;
  $filename = $path[$style]["tpl"]."/classes.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  $rep_cont = ""; $tmp_attr = "";

  $_GET["uid"] = substr($_GET["uid"], 2);
  //folder alignment
  $tmp_attr = str_replace("%CLASS_ID%", "ala".$_GET["uid"], $matches[1]);
  $tmp_attr = str_replace("%CLASS_NM%", getMessage("Alignment"), $tmp_attr);
  $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
  $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=alignmentlistdata&amp;sid=".
  $session_id."&amp;uid=ala".$_GET["uid"], $tmp_attr);
  if (getAlignment("at".$_GET["uid"], $matches, $session_id) != "")
    $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=alignmentlist&amp;sid=".
      $session_id."&amp;uid=ala".$_GET["uid"], $tmp_attr);
  else
    $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);

  $contents = str_replace($matches[0], $tmp_attr, $contents);
  header ($header["xml"]);
  return $contents;

};

function AlignmentListFolderData($mes) {
  global $_GET, $path, $style, $session_id, $data, $term_Thing_ID, $language, $header;
  $filename = $path[$style]["tpl"]."/attrdata.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);

  $udata = str_replace($datmatches[0], "", $matches[1]);
  $udata = str_replace("%GROUP_ID%", "0", $udata);
  $udata = str_replace("%GROUP_TL%", $mes, $udata);
  $contents = str_replace("%DatHref%", "index.php?trg=ontoedit&amp;act=alignmentlistdata&amp;sid=".
      $session_id."&amp;uid=".$_GET["uid"], $contents);
  header ($header["xml"]);
  return str_replace($matches[0], $udata, $contents);
}

function alignmentList(){
 global $_GET, $data, $path, $style, $session_id, $language, $header;
  $filename = $path[$style]["tpl"]."/classes.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  $rep_cont = "";
  
  $classID = substr($_GET["uid"], 3);
  
  if (substr($_GET["uid"], 0, 3) == "alc")
    $rep_cont = getAlignment($classID, $matches, $session_id);
  else
    $rep_cont = getAlignment("at".$classID, $matches, $session_id);
  
  $contents = str_replace($matches[0], $rep_cont, $contents);
  header ($header["xml"]);

  return $contents;
};

function getAlignment($class_id, $matches, $session_id) {
  global $_GET, $undef_id;
  $rep_cont = "";
  if (substr($class_id, 0, 2) != "at"){
    $sql_str = "SELECT t.text FROM class c, term t WHERE t.id=c.term_id and c.id=".$class_id;
    getData($sql_str, $next_data);

    $sql_str = "SELECT * FROM alignment a, term t, class c WHERE a.class2_id=c.id and c.term_id=t.id and attr2_id=0 and a.class1_id=".$class_id;
    $i_max = getMultData($sql_str, $ontosdata);
  
    $sql_str = "SELECT * FROM alignment a, term t WHERE t.id=".$undef_id." and a.class2_id=-1 and attr2_id=0 and a.class1_id=".$class_id;
    if (getData($sql_str, $ontosdata1) == 1){
      $ontosdata[$i_max]["class2_id"] = $ontosdata1["class2_id"];
      $ontosdata[$i_max]["text"] = $ontosdata1["text"];
      $i_max++;
    };
  }else{
    $sql_str = "SELECT t.text FROM class c, term t WHERE t.id=c.term_id and c.id=".substr($class_id, 2);
    getData($sql_str, $next_data);

    $sql_str = "SELECT * FROM alignment a, term t, class c WHERE a.class2_id=c.id and c.term_id=t.id and attr2_id=0 and a.attr1_id=".substr($class_id, 2);
    $i_max = getMultData($sql_str, $ontosdata);
  
    $sql_str = "SELECT * FROM alignment a, term t WHERE t.id=".$undef_id." and a.class2_id=-1 and attr2_id=0 and a.attr1_id=".substr($class_id, 2);
    if (getData($sql_str, $ontosdata1) == 1){
      $ontosdata[$i_max]["class2_id"] = $ontosdata1["class2_id"];
      $ontosdata[$i_max]["text"] = $ontosdata1["text"];
      $i_max++;
    };
  };

  for ($i = 0; $i < $i_max; $i++) {
    $tmp_attr = str_replace("%CLASS_NM%", $ontosdata[$i]["text"], $matches[1]);
    if ($ontosdata[$i]["class2_id"] == -1)
      $tmp_attr = str_replace("%IMG_TYPE%", "undef", $tmp_attr);
    else
      $tmp_attr = str_replace("%IMG_TYPE%", "class", $tmp_attr);
  
    if (substr($class_id, 0, 2) != "at"){
      $tmp_attr = str_replace("%CLASS_ID%", "alc".$class_id."alc".$ontosdata[$i]["class2_id"], $tmp_attr);
  
      if (isset($_GET["sel"]) && $_GET["sel"] == "alc".$class_id."alc".$ontosdata[$i]["class2_id"])
        $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);

      $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=alignmentdata&amp;sid=".
      $session_id."&amp;uid="."alc".$class_id."alc".$ontosdata[$i]["class2_id"], $tmp_attr);
      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
      if (isset($_GET["sel"]) && 
        ($_GET["sel"] == "alc".$class_id."alc".$ontosdata[$i]["class2_id"] ||
        ($_GET["sel"] == 0 && $i == 0))) {
        $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);
      }
    }else{
      $tmp_attr = str_replace("%CLASS_ID%", "ala".substr($class_id, 2)."alc".$ontosdata[$i]["class2_id"], $tmp_attr);
  
      if (isset($_GET["sel"]) && $_GET["sel"] == "ala".substr($class_id, 2)."alc".$ontosdata[$i]["class2_id"])
        $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);

      $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=alignmentdata&amp;sid=".
      $session_id."&amp;uid="."ala".substr($class_id, 2)."alc".$ontosdata[$i]["class2_id"], $tmp_attr);
      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
      if (isset($_GET["sel"]) && 
        ($_GET["sel"] == "ala".substr($class_id, 2)."alc".$ontosdata[$i]["class2_id"] ||
        ($_GET["sel"] == 0 && $i == 0))) {
        $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);
      }
    };

    $rep_cont .= $tmp_attr;
  };

  if (substr($class_id, 0, 2) != "at")
    $sql_str = "SELECT * FROM alignment a, term t, attr at WHERE a.attr2_id=at.id and at.term_id=t.id and a.class2_id=-1 and attr2_id > 0 and a.class1_id=".$class_id;
  else
    $sql_str = "SELECT * FROM alignment a, term t, attr at WHERE a.attr2_id=at.id and at.term_id=t.id and a.class2_id=-1 and attr2_id > 0 and a.attr1_id=".substr($class_id, 2);

  $i_max = getMultData($sql_str, $ontosdata);

  for ($i = 0; $i < $i_max; $i++) {
    if (substr($class_id, 0, 2) != "at"){
      $sql_tmp = "SELECT t.text ".
             "FROM term t, class c, class_attr ca ".
             "WHERE c.id=ca.class_id and t.id=c.term_id and ca.attr_id=".$ontosdata[$i]["attr2_id"];
      //echo $sql_tmp;
      getData($sql_tmp, $class_name);

      $tmp_attr = str_replace("%CLASS_ID%", "alc".$class_id."ala".$ontosdata[$i]["attr2_id"], $matches[1]);
  
      if (isset($_GET["sel"]) && $_GET["sel"] == "alc".$class_id."ala".$ontosdata[$i]["attr2_id"])
        $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);
  
      $tmp_attr = str_replace("%CLASS_NM%", $class_name["text"].$ontosdata[$i]["text"], $tmp_attr);
      $tmp_attr = str_replace("%IMG_TYPE%", "attr", $tmp_attr);
      $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=alignmentdata&amp;sid=".
      $session_id."&amp;uid="."alc".$class_id."ala".$ontosdata[$i]["class2_id"], $tmp_attr);
      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
  
      if (isset($_GET["sel"]) && 
        ($_GET["sel"] == "alc".$class_id."ala".$ontosdata[$i]["attr2_id"] ||
        ($_GET["sel"] == 0 && $i == 0))) {
        $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);
      }
    }else{
      //echo "ala".substr($class_id, 2)."ala".$ontosdata[$i]["attr2_id"];
      $sql_tmp = "SELECT t.text ".
             "FROM term t, class c, class_attr ca ".
             "WHERE c.id=ca.class_id and t.id=c.term_id and ca.attr_id=".$ontosdata[$i]["attr2_id"];
      //echo $sql_tmp;
      getData($sql_tmp, $class_name);

      $tmp_attr = str_replace("%CLASS_ID%", "ala".substr($class_id, 2)."ala".$ontosdata[$i]["attr2_id"], $matches[1]);
  
      if (isset($_GET["sel"]) && $_GET["sel"] == "ala".substr($class_id, 2)."ala".$ontosdata[$i]["attr2_id"])
        $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);
  
      $tmp_attr = str_replace("%CLASS_NM%", $class_name["text"]." -> ".$ontosdata[$i]["text"], $tmp_attr);
      $tmp_attr = str_replace("%IMG_TYPE%", "attr", $tmp_attr);
      $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=alignmentdata&amp;sid=".
      $session_id."&amp;uid="."ala".substr($class_id, 2)."ala".$ontosdata[$i]["attr2_id"], $tmp_attr);
      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
  
      if (isset($_GET["sel"]) && 
        ($_GET["sel"] == "ala".substr($class_id, 2)."ala".$ontosdata[$i]["attr2_id"] ||
        ($_GET["sel"] == 0 && $i == 0))) {
        $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);
      }
    };
    $rep_cont .= $tmp_attr;
  };
  return $rep_cont;
}

function alignmentData() {
  global $_GET, $data, $path, $style, $session_id, $header;
  
  $temp_uid = $_GET["uid"];
  $_GET["uid"] = substr($_GET["uid"], 3);

  if (strpos($_GET["uid"], "ala") != false)
    $ID = explode("ala", $_GET["uid"]);

  if (strpos($_GET["uid"], "alc") != false)
    $ID = explode("alc", $_GET["uid"]);

  if (substr($temp_uid, 0, 3) == "alc")
    $ID[2] = "alc";
  else
    $ID[2] = "ala";

  $contents = getAlignmentData($ID, "");
  header ($header["xml"]);
  return $contents;
}

function getAlignmentClass($classID, $attrID) {
  $out_data["classID"] = $classID;
  $out_data["attr_class_ID"] = 0;
  $sql_str = "SELECT id FROM class_attr WHERE class_id = ".$classID." AND attr_id = ".$attrID.";";
  if (getData($sql_str, $attr_class_id) == 1) $out_data["attr_class_ID"] = $attr_class_id["id"];
  else {
    $sql_str = "SELECT parent_id FROM class WHERE id = ".$classID.";";
    if (getData($sql_str, $p_class) == 1 && $p_class["parent_id"] != 0) {
      $out_data = getAlignmentClass($p_class["parent_id"], $attrID);
    }
  }
  return $out_data;
}

function updateAlignment() {
  global $_GET, $data, $path, $style, $session_id, $term_Thing_ID, $language, $header;

  $temp_uid = $_GET["uid"];
  $_GET["uid"] = substr($_GET["uid"], 3);
  if (strpos($_GET["uid"], "ala") != false)
    $ID = explode("ala", $_GET["uid"]);

  if (strpos($_GET["uid"], "alc") != false)
    $ID = explode("alc", $_GET["uid"]);

  if (substr($temp_uid, 0, 3) == "alc")
    $ID[2] = "alc";
  else
    $ID[2] = "ala";

  $comment = "";
  if ($data["type"] >= 1) {
    if (isset($_GET["otype"]))
      $_GET["otype"] = utf8Encode($_GET["otype"]);
    else
      $_GET["otype"] = 0;
    if (isset($_GET["aotype"]))
      $_GET["aotype"] = utf8Encode($_GET["aotype"]);
    else
      $_GET["aotype"] = 0;
      
    //$attr_data = getAttrClass($ID[0], $ID[1]);
    if ($_GET["otype"] > 0) {
        if ($ID[2] == "alc"){
          $sql_str = "UPDATE Alignment SET class2_id=".$_GET["otype"]." WHERE class1_id=".$ID[0]." and class2_id=-1 and attr2_id=0";
          $comment = "cc".$_GET["otype"];
        }else{
          $sql_str = "UPDATE Alignment SET class2_id=".$_GET["otype"]." WHERE attr1_id=".$ID[0]." and class2_id=-1 and attr2_id=0";
          $comment = "ac".$_GET["otype"];
        };
        insertRecord($sql_str);
        
    };
    if ($_GET["aotype"] > 0) {
        //echo $_GET["aotype"];
        if ($ID[2] == "alc"){
          $sql_str = "UPDATE Alignment SET attr2_id=".$_GET["aotype"]." WHERE class1_id=".$ID[0]." and class2_id=-1 and attr2_id=0";
          $comment = "ca".$_GET["aotype"];
        }else{
          $sql_str = "UPDATE Alignment SET attr2_id=".$_GET["aotype"]." WHERE attr1_id=".$ID[0]." and class2_id=-1 and attr2_id=0";
          $comment = "aa".$_GET["aotype"];
        };
        insertRecord($sql_str);
    };

  };
  $contents = getAlignmentData($ID, $comment);
  header ($header["xml"]);
  return $contents;
}

function createAlignment() {
  global $_GET, $data, $path, $style, $session_id, $language, $header;
  $alaalc = "ala";
  if (substr($_GET["uid"], 0, 3) == "alc")
    $alaalc = "alc";
    
  $_GET["uid"] = substr($_GET["uid"], 3);
  if (strpos($_GET["uid"], "ala") != false)
    $ID = explode("ala", $_GET["uid"]);

  if (strpos($_GET["uid"], "alc") != false)
    $ID = explode("alc", $_GET["uid"]);
  
  if ((strpos($_GET["uid"], "alc") == false)&&(strpos($_GET["uid"], "ala") == false))
    $ID[0] = $_GET["uid"];
  
  $_GET["uid"] = $ID[0];

  if ($data["type"] >= 1) { //user
      $filename = $path[$style]["tpl"]."/action.xml";
      $fp = fopen($filename, "r");
      $contents = fread($fp, filesize ($filename));
      fclose ($fp);
      preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
      
      //echo "SELECT * FROM alignment WHERE class1_id=".$_GET["uid"]." and class2_id=-1";
      $sql_tmp1 = "SELECT * FROM alignment WHERE class1_id=".$_GET["uid"]." and class2_id=-1 and attr2_id=0";
      $sql_tmp2 = "SELECT * FROM alignment WHERE attr1_id=".$_GET["uid"]." and class2_id=-1 and attr2_id=0";
      if (((getdata($sql_tmp1, $data) != 1)&&($alaalc == "alc"))||((getdata($sql_tmp2, $data) != 1)&&($alaalc == "ala"))){
          if ($alaalc == "alc"){
            insertRecord("INSERT INTO alignment (class1_id, class2_id, session_id) VALUES(".
              $_GET["uid"].", -1, '".
              $session_id."');");
            
            if (getData("SELECT id FROM alignment where session_id = '".$session_id."';", $datatmp) == 1) {
              $sql_str = "UPDATE alignment SET session_id = '' WHERE id=".$datatmp["id"].";";
              insertRecord($sql_str);
            };
          }else{
            insertRecord("INSERT INTO alignment (attr1_id, class1_id, class2_id, session_id) VALUES(".
              $_GET["uid"].", -1, -1, '".
              $session_id."');");
            
            if (getData("SELECT id FROM alignment where session_id = '".$session_id."';", $datatmp) == 1) {
              $sql_str = "UPDATE alignment SET session_id = '' WHERE id=".$datatmp["id"].";";
              insertRecord($sql_str);
            };
          };
          $contents = str_replace("%SCCSS%", "1", $contents);
      }else{
          $contents = str_replace("%SCCSS%", "0", $contents);
          $datatmp["id"] = 0;
      };
  } else {
    $contents = str_replace("%SCCSS%", "0", $contents);
    $datatmp["id"] = 0;
  }
  
  if ($alaalc == "alc"){
    $tmp_cont = str_replace("%NODE_ID%", "alc".$_GET["uid"]."alc-1", $matches[1]);
    $tmp_cont = str_replace("%NODE_NM%", getMessage("Name"), $tmp_cont);
    $tmp_cont = str_replace("%IMG_TYPE%", "undef", $tmp_cont);
    $tmp_cont = str_replace("%NODE_LN%", "index.php?trg=ontoedit&amp;act=alignmentdata&amp;sid=".
      $session_id."&amp;uid=alc-1", $tmp_cont);
    $contents = str_replace($matches[0], $tmp_cont, $contents);
    $contents = str_replace("%XMLSRC%", "index.php?trg=ontoedit&amp;act=alignmentlist&amp;sid=".
      $session_id."&amp;uid=alc".$_GET["uid"]."&amp;sel=alc".$_GET["uid"]."alc-1", $contents);
    $contents = str_replace("%ACT_ID%", "Add", $contents);
    $contents = str_replace("%PARENT%", "alc".$_GET["uid"], $contents);
  }else{
    $tmp_cont = str_replace("%NODE_ID%", "ala".$_GET["uid"]."alc-1", $matches[1]);
    $tmp_cont = str_replace("%NODE_NM%", getMessage("Name"), $tmp_cont);
    $tmp_cont = str_replace("%IMG_TYPE%", "undef", $tmp_cont);
    $tmp_cont = str_replace("%NODE_LN%", "index.php?trg=ontoedit&amp;act=alignmentdata&amp;sid=".
      $session_id."&amp;uid=ala-1", $tmp_cont);
    $contents = str_replace($matches[0], $tmp_cont, $contents);
    $contents = str_replace("%XMLSRC%", "index.php?trg=ontoedit&amp;act=alignmentlist&amp;sid=".
      $session_id."&amp;uid=ala".$_GET["uid"]."&amp;sel=ala".$_GET["uid"]."alc-1", $contents);
    $contents = str_replace("%ACT_ID%", "Add", $contents);
    $contents = str_replace("%PARENT%", "ala".$_GET["uid"], $contents);
  };
  header ($header["xml"]);
  return $contents;}

function deleteAlignment($ID) {
  global $data, $path, $style, $session_id;

  $filename = $path[$style]["tpl"]."/action.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);

  if ($data["type"] >= 1 && isset($ID[1])){
      //delete from class_attr
      if ($ID[3] == "alc"){
        if ($ID[2] == "ala"){
          insertRecord("DELETE FROM Alignment WHERE class1_id=".$ID[0]." and class2_id=-1 and attr2_id=".$ID[1]);
        };
        if ($ID[2] == "alc"){
          insertRecord("DELETE FROM Alignment WHERE class1_id=".$ID[0]." and class2_id=".$ID[1]." and attr2_id=0");
        };
      }else{
        if ($ID[2] == "ala"){
          insertRecord("DELETE FROM Alignment WHERE attr1_id=".$ID[0]." and class2_id=-1 and attr2_id=".$ID[1]);
        };
        if ($ID[2] == "alc"){
          insertRecord("DELETE FROM Alignment WHERE attr1_id=".$ID[0]." and class2_id=".$ID[1]." and attr2_id=0");
        };
      };

      $contents = str_replace("%SCCSS%", "1", $contents);
   }else {
    $contents = str_replace("%SCCSS%", "0", $contents);
    $ID[1] = 0; $ID[2] = "alc";
  }
  $contents = str_replace($matches[0], "", $contents);
  $contents = str_replace("%ACT_ID%", "Delete", $contents);
  if ($ID[3] == "alc"){
    if ($ID[2] == "ala")
      $contents = str_replace("%PARENT%", "alc".$ID[0]."ala".$ID[1], $contents);
    if ($ID[2] == "alc")
      $contents = str_replace("%PARENT%", "alc".$ID[0]."alc".$ID[1], $contents);
      
    $contents = str_replace("%SELECT%", "alc".$ID[0], $contents);
  }else{
    if ($ID[2] == "ala")
      $contents = str_replace("%PARENT%", "ala".$ID[0]."ala".$ID[1], $contents);
    if ($ID[2] == "alc")
      $contents = str_replace("%PARENT%", "ala".$ID[0]."alc".$ID[1], $contents);
      
    $contents = str_replace("%SELECT%", "ala".$ID[0], $contents);
  };
  return $contents;
}


function getAlignmentData($ID, $comment) {
  global $_GET, $path, $style, $session_id, $data, $term_Thing_ID, $language;
  $filename = $path[$style]["tpl"]."/attrdata.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);
  preg_match("/\%REPOPT\%(.*?)\%REPOPT\%/s", $datmatches[1], $optmatches);
  $rep_cont = "";
  $attr_data = getAlignmentClass($ID[0], $ID[1]);
  //print("AAA");

  getData("SELECT domtype_id, min, max, limited FROM class_attr ".
          "WHERE id = ".$attr_data["attr_class_ID"].";",
          $domaindata); //domain, min, max, etc.

  getData("SELECT term.id as term_id, term.text as name, term.description as description ".
          "FROM attr, term ".
          "WHERE attr.id = ".$ID[1].
          " AND attr.term_id = term.id AND term.lang_id = ".$language["content"].
          ";",
          $ontodata); //name, descr
  if ($ID[0] != $attr_data["classID"])
    getData("SELECT term.text as name FROM class, term ".
            "WHERE class.id = ".$attr_data["classID"].
            " AND class.term_id = term.id AND term.lang_id = ".$language["content"].
            ";",
            $iontodata); //class the attribute is inherited from

  if ($ID[2] == "alc"){
    getData("SELECT term.text as name, class.onto_id as onto_id FROM class, term ".
            "WHERE class.id = ".$ID[0].
            " AND class.term_id = term.id AND term.lang_id = ".$language["content"].
            ";",
            $contodata); //current class
  }else{
    getData("SELECT t.text as name, a.onto_id as onto_id FROM attr a, term t ".
            "WHERE a.id = ".$ID[0].
            " AND a.term_id = t.id AND t.lang_id = ".$language["content"].
            ";",
            $contodata); //current class
  };


  getData("SELECT term.text as name FROM ontology, term ".
          "WHERE ontology.id = ".$contodata["onto_id"].
          " AND ontology.term_id = term.id AND term.lang_id = ".$language["content"].
          ";",
          $pontodata); //current ontology

  $i_max = getMultData("SELECT domtype.id as id, term.text as name FROM domtype, term ".
        "WHERE domtype.term_id = term.id AND term.lang_id = ".$language["content"].";",
        $domtypes); //possible domains

  $udata = "";
  if ($data["type"] >= 1) { //user
    //New name
    if (($comment > -1)&&($comment != "")){
      if (substr($comment, 0, 2) == "cc"){
        getData("SELECT t.text FROM class c, term t WHERE c.term_id=t.id and c.id=".substr($comment, 2),
                $new_class);
        $contents = str_replace("%IMG%", "class", $contents);
        $contents = str_replace("%ID%", "alc".$ID[0]."alc".substr($comment, 2), $contents);
        $contents = str_replace("%NAME%", $new_class["text"], $contents);
      };
      if (substr($comment, 0, 2) == "ca"){
        getData("SELECT t.text FROM attr a, term t WHERE a.term_id=t.id and a.id=".substr($comment, 2),
                $new_class);
        $contents = str_replace("%IMG%", "attr", $contents);
        $contents = str_replace("%ID%", "alc".$ID[0]."ala".substr($comment, 2), $contents);
        $sql_tmp = "SELECT t.text ".
               "FROM term t, class c, class_attr ca ".
               "WHERE c.id=ca.class_id and t.id=c.term_id and ca.attr_id=".substr($comment, 2);
        //echo $sql_tmp;
        getData($sql_tmp, $class_name);
        $contents = str_replace("%NAME%", $class_name["text"]." -> ".$new_class["text"], $contents);
      };
      if (substr($comment, 0, 2) == "ac"){
        getData("SELECT t.text FROM class c, term t WHERE c.term_id=t.id and c.id=".substr($comment, 2),
                $new_class);
        $contents = str_replace("%IMG%", "class", $contents);
        $contents = str_replace("%ID%", "ala".$ID[0]."alc".substr($comment, 2), $contents);
      };
      if (substr($comment, 0, 2) == "aa"){
        getData("SELECT t.text FROM attr a, term t WHERE a.term_id=t.id and a.id=".substr($comment, 2),
                $new_class);
        $contents = str_replace("%IMG%", "attr", $contents);
        $contents = str_replace("%ID%", "ala".$ID[0]."ala".substr($comment, 2), $contents);
        $sql_tmp = "SELECT t.text ".
               "FROM term t, class c, class_attr ca ".
               "WHERE c.id=ca.class_id and t.id=c.term_id and ca.attr_id=".substr($comment, 2);
        //echo $sql_tmp;
        getData($sql_tmp, $class_name);
        $contents = str_replace("%NAME%", $class_name["text"]." -> ".$new_class["text"], $contents);
      };
    };

    //Class (Attr) Name
    $classlb = str_replace($optmatches[0], "", $datmatches[1]);
    $classlb = str_replace("%TYPE%", "Label", $classlb);
    $classlb = str_replace("%NODE_ID%", "cname", $classlb);
    if ($ID[2] == "alc")
      $classlb = str_replace("%NODE_TL%", getMessage("Class Name"), $classlb);
    else
      $classlb = str_replace("%NODE_TL%", getMessage("Attributes Name"), $classlb);
    $classlb = str_replace("%VALUE%", $contodata["name"], $classlb);
    $iclasslb = "";

    $sql_tmp1 = "SELECT * FROM Alignment WHERE class1_id=".$ID[0]." and class2_id=-1 and attr2_id=0";
    $sql_tmp2 = "SELECT * FROM Alignment WHERE attr1_id=".$ID[0]." and class2_id=-1 and attr2_id=0";
    
    if (((((getData($sql_tmp1, $aligndata) == 1)&&($ID[2] == "alc"))||((getData($sql_tmp2, $aligndata) == 1)&&($ID[2] == "ala")))&&($ID[1] == -1))&&($ID[1] == -1)){
      $fname = "";
      if ($ID[2] == "alc")
        $sql_tmp = "SELECT onto_id FROM class WHERE id = ".$ID[0];
      else
        $sql_tmp = "SELECT onto_id FROM attr WHERE id = ".$ID[0];
      getData($sql_tmp, $onto_id);

      $sql_tmp = "SELECT type_id FROM ontology WHERE id = ".$onto_id["onto_id"];
      getData($sql_tmp, $onto_type_id);

      if ($onto_type_id["type_id"] == "5")
        $sql_tmp = "SELECT DISTINCT c.base_onto_id FROM class c, ontology o WHERE o.type_id=1 and o.id=c.base_onto_id and c.onto_id=".$onto_id["onto_id"];
      else
        $sql_tmp = "SELECT DISTINCT c.base_onto_id FROM class c, ontology o WHERE o.type_id=2 and o.id=c.base_onto_id and c.onto_id=".$onto_id["onto_id"];
      getData($sql_tmp, $base_onto_id);
      
      if ($onto_type_id["type_id"] == "5"){
        //Alignment class
        if ($ID[2] == "alc"){
          $sql_tmp = "SELECT c.id, t.text ".
                      "FROM class c, term t ".
                      "WHERE t.id=c.term_id ".
                      "and onto_id=".$onto_id["onto_id"]." ".
                      "and base_onto_id=".$base_onto_id["base_onto_id"]." ".
                      "and c.id not in (SELECT class2_id FROM Alignment WHERE class1_id=".$ID[0].")";
          //echo $sql_tmp;
          $ontotype = Array();
          $i_max = getMultData($sql_tmp, $clases);
        }else{
          $sql_tmp = "SELECT onto_id FROM attr WHERE id = ".$ID[0];
          $ontotype = Array();
          getData($sql_tmp, $onto_id);
  
          //$sql_tmp = "SELECT DISTINCT a.baseonto_id FROM attr a, ontology o WHERE o.type_id=1 and o.id=a.baseonto_id and a.onto_id=".$onto_id["onto_id"];
          $sql_tmp = "SELECT DISTINCT a.baseonto_id FROM attr a, ontology o WHERE (o.type_id=1 or o.type_id=0) and o.id=a.baseonto_id and a.onto_id=".$onto_id["onto_id"];
          $ontotype = Array();
          getData($sql_tmp, $base_onto_id);
          $sql_tmp = "SELECT c.id, t.text ".
                      "FROM class c, term t ".
                      "WHERE t.id=c.term_id ".
                      "and onto_id=".$onto_id["onto_id"]." ".
                      "and base_onto_id=".$base_onto_id["baseonto_id"]." ".
                      "and c.id not in (SELECT class2_id FROM Alignment WHERE attr1_id=".$ID[0].")";
          //echo $sql_tmp;
          $ontotype = Array();
          $i_max = getMultData($sql_tmp, $clases);

        };
    
        $opts = "";
        for ($i = 0; $i < $i_max; $i++) {
          $opt = str_replace("%OPT_ID%", $clases[$i]["id"], $optmatches[1]);
          $opt = str_replace("%OPT_TL%", $clases[$i]["text"], $opt);
          $opts .= $opt;
        };
    
        //domain
        $fname = str_replace($optmatches[0], $opts, $datmatches[1]);
        $fname = str_replace("%TYPE%", "Option", $fname);
        $fname = str_replace("%NODE_ID%", "otype", $fname);
        $fname = str_replace("%NODE_TL%", getMessage("Alignment Class"), $fname);
      };
      
      $clases = Array();
      
      
      //Alignment attributes
      if ($ID[2] == "alc"){
        $sql_tmp = "SELECT onto_id FROM class WHERE id = ".$ID[0];
        $ontotype = Array();
        getData($sql_tmp, $onto_id);

        $sql_tmp = "SELECT DISTINCT c.base_onto_id ".
                   "FROM class c, ontology o ".
                   "WHERE o.type_id=1 ".
                     "and o.id=c.base_onto_id ".
                     "and c.onto_id=".$onto_id["onto_id"];

        //echo $sql_tmp;
        getData($sql_tmp, $base_onto_id);
    
        $sql_tmp = "SELECT a.id, t.text, c.class_id  ".
                   "FROM attr a, term t, class_attr c ".
                   "WHERE t.id=a.term_id ".
                     "and c.attr_id=a.id ".
                     "and a.onto_id=".$onto_id["onto_id"]." ".
                     "and a.baseonto_id=".$base_onto_id["base_onto_id"]." ".
                     "and a.id not in (SELECT attr2_id FROM Alignment WHERE class1_id=".$ID[0].")";
        //echo $sql_tmp;
        $i_max = getMultData($sql_tmp, $clases);
      }else{
        $sql_tmp = "SELECT onto_id FROM attr WHERE id = ".$ID[0];
        $ontotype = Array();
        getData($sql_tmp, $onto_id);

        $sql_tmp = "SELECT o.type_id FROM attr a, ontology o WHERE a.baseonto_id=o.id and a.id=".$ID[0];
        getData($sql_tmp, $base_onto_type_id);
        
        if (($onto_type_id["type_id"] == "5")||($base_onto_type_id["type_id"] == "2"))
          $sql_tmp = "SELECT DISTINCT a.baseonto_id ".
                     "FROM attr a, ontology o ".
                     "WHERE (o.type_id=1 or o.type_id=0)".
                       "and o.id=a.baseonto_id ".
                       "and a.onto_id=".$onto_id["onto_id"];
        else
          $sql_tmp = "SELECT DISTINCT a.baseonto_id ".
                     "FROM attr a, ontology o ".
                     "WHERE o.type_id=2".
                       "and o.id=a.baseonto_id ".
                       "and a.onto_id=".$onto_id["onto_id"];

        //echo $sql_tmp;
        getData($sql_tmp, $base_onto_id);
    
        $sql_tmp = "SELECT a.id, t.text, c.class_id ".
                   "FROM attr a, term t, class_attr c ".
                   "WHERE t.id=a.term_id ".
                     "and c.attr_id=a.id ".
                     "and a.onto_id=".$onto_id["onto_id"]." ".
                     "and a.baseonto_id=".$base_onto_id["baseonto_id"]." ".
                     "and a.id not in (SELECT attr2_id FROM Alignment WHERE attr1_id=".$ID[0].")";
        //echo $sql_tmp;
        $i_max = getMultData($sql_tmp, $clases);
      };
  
      $opts = "";
      for ($i = 0; $i < $i_max; $i++) {
        $sql_tmp = "SELECT t.text ".
                   "FROM term t, class c ".
                   "WHERE t.id=c.term_id and c.id=".$clases[$i]["class_id"];
        //echo $sql_tmp;
        getData($sql_tmp, $class_name);
        $opt = str_replace("%OPT_ID%", $clases[$i]["id"], $optmatches[1]);
        $opt = str_replace("%OPT_TL%", $class_name["text"]." -> ".$clases[$i]["text"], $opt);
        $opts .= $opt;
      };
  
      //domain
      $afname = str_replace($optmatches[0], $opts, $datmatches[1]);
      $afname = str_replace("%TYPE%", "Option", $afname);
      $afname = str_replace("%NODE_ID%", "aotype", $afname);
      $afname = str_replace("%NODE_TL%", getMessage("Alignment Attributes"), $afname);
    };
    $sql_tmp = "SELECT onto_id FROM class WHERE id = ".$ID[0];
    getData($sql_tmp, $onto_id);
    $ubutn = "";
    if ($onto_id["onto_id"] == "5"){
      //button for classes
      $ubutn = str_replace($optmatches[0], "", $datmatches[1]);
      $ubutn = str_replace("%TYPE%", "Button", $ubutn);
      $ubutn = str_replace("%NODE_ID%", "obutn", $ubutn);
      $ubutn = str_replace("%NODE_TL%", getMessage("Save"), $ubutn);
      $ubutn = str_replace("%VALUE%", "'0','otype'", $ubutn);
    };

    //button for attributes
    $aubutn = str_replace($optmatches[0], "", $datmatches[1]);
    $aubutn = str_replace("%TYPE%", "Button", $aubutn);
    $aubutn = str_replace("%NODE_ID%", "obutn", $aubutn);
    $aubutn = str_replace("%NODE_TL%", getMessage("Save"), $aubutn);
    $aubutn = str_replace("%VALUE%", "'0','aotype'", $aubutn);

    //Ontology Name
    $ontolb = str_replace($optmatches[0], "", $datmatches[1]);
    $ontolb = str_replace("%TYPE%", "Label", $ontolb);
    $ontolb = str_replace("%NODE_ID%", "lname", $ontolb);
    $ontolb = str_replace("%NODE_TL%", getMessage("Ontology Name"), $ontolb);
    $ontolb = str_replace("%VALUE%", $pontodata["name"], $ontolb);

    $sql_tmp1 = "SELECT * FROM Alignment WHERE class1_id=".$ID[0]." and class2_id=-1 and attr2_id=0";
    $sql_tmp2 = "SELECT * FROM Alignment WHERE attr1_id=".$ID[0]." and class2_id=-1 and attr2_id=0";
    
    if (((((getData($sql_tmp1, $aligndata) == 1)&&($ID[2] == "alc"))||((getData($sql_tmp2, $aligndata) == 1)&&($ID[2] == "ala")))&&($ID[1] == -1))&&($ID[1] == -1))
      $udata = str_replace($datmatches[0], $classlb.$iclasslb.$ontolb.$fname.$ubutn.$afname.$aubutn, $matches[1]);
    else
      $udata = str_replace($datmatches[0], $classlb.$iclasslb.$ontolb, $matches[1]);
    $udata = str_replace("%GROUP_ID%", "0", $udata);
    $udata = str_replace("%GROUP_TL%", getMessage("Alignment data"), $udata);
    //print "****".$udata."****";

//Action
    $btn1 = str_replace($optmatches[0], "", $datmatches[1]);
    $btn1 = str_replace("%TYPE%", "Button", $btn1);
    $btn1 = str_replace("%NODE_ID%", "", $btn1);
    $btn1 = str_replace("%NODE_TL%", getMessage("Ontologies"), $btn1);
    $btn1 = str_replace("%VALUE%", "'wd_tree', 'index.php?trg=ontoedit&amp;sid=".
      $session_id."&amp;act=ontolist&amp;sel=".$contodata["onto_id"]."'", $btn1);
      
    $btns = str_replace($datmatches[0], $btn1, $matches[1]);
    $btns = str_replace("%GROUP_ID%", "1", $btns);
    $btns = str_replace("%GROUP_TL%", getMessage("Action"), $btns);

  }
  else { //guest
    //New name
    $contents = str_replace("%NAME%", $ontodata["name"], $contents);
    //Name
    $fname = str_replace($optmatches[0], "", $datmatches[1]);
    $fname = str_replace("%TYPE%", "Label", $fname);
    $fname = str_replace("%NODE_ID%", "oname", $fname);
    $fname = str_replace("%NODE_TL%", getMessage("Name"), $fname);
    $fname = str_replace("%VALUE%", $ontodata["name"], $fname);
    //Description
    $lname = str_replace($optmatches[0], "", $datmatches[1]);
    $lname = str_replace("%TYPE%", "Label", $lname);
    $lname = str_replace("%NODE_ID%", "odescr", $lname);
    $lname = str_replace("%NODE_TL%", getMessage("Description"), $lname);
    $lname = str_replace("%VALUE%", $ontodata["description"], $lname);
    //User
    $udata = str_replace($datmatches[0], $fname.$lname, $matches[1]);
    $udata = str_replace("%GROUP_ID%", "0", $udata);
    $udata = str_replace("%GROUP_TL%", getMessage("Attribute data"), $udata);
    //print "****".$udata."****";
  }
  return str_replace($matches[0], $udata.$btns, $contents);
}

function CompareAl($ar1, $ar2)
{
  $res = strcasecmp($ar1['name'], $ar2['name']);
  if ($res < 0) return -1;
  else if ($res > 0) return 1;
  return 0;
}

?>